package com.hue.gulimail.gulimailproduct.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hue.gulimail.gulimailproduct.entity.AttrAttrgroupRelationEntity;
import com.hue.gulimail.gulimailproduct.entity.AttrEntity;
import com.hue.gulimail.gulimailproduct.service.AttrAttrgroupRelationService;
import com.hue.gulimail.gulimailproduct.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.Query;

import com.hue.gulimail.gulimailproduct.dao.AttrGroupDao;
import com.hue.gulimail.gulimailproduct.entity.AttrGroupEntity;
import com.hue.gulimail.gulimailproduct.service.AttrGroupService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * @author yfq
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrService attrService;

//    @Autowired
//    private Attr

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer catelogId) {
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    new QueryWrapper<AttrGroupEntity>()
            );
            return new PageUtils(page);
        }
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catelogId);
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(obj -> obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrgroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> relationWrapper = new QueryWrapper<>();
        relationWrapper.eq("attr_group_id", attrgroupId);
        List<AttrAttrgroupRelationEntity> relationList = attrAttrgroupRelationService.list(relationWrapper);
        List<AttrEntity> attrList = new ArrayList<>();
        for (AttrAttrgroupRelationEntity relation : relationList) {
            AttrEntity attr = attrService.getById(relation.getAttrId());
            attrList.add(attr);
        }
        return attrList;
    }

    @Override
    public PageUtils getNoAttrRelation(Long attrgroupId, @RequestParam Map<String, Object> params) {
        //获取所有的基本属性
        QueryWrapper<AttrEntity> attrWrapper = new QueryWrapper<>();
        attrWrapper.eq("attr_type", 1);
        Object key = params.get("key");
        if (key != null && !StringUtils.isEmpty(key.toString())) {
            attrWrapper.and(obj -> obj.eq("attr_id", key).or().like("attr_name", key));
        }
        List<AttrEntity> attrList = attrService.list(attrWrapper);
        //过滤掉这个属性分组已经关联的属性
        QueryWrapper<AttrAttrgroupRelationEntity> relationWrapper = new QueryWrapper<>();
        relationWrapper.eq("attr_group_id", attrgroupId);
        List<AttrAttrgroupRelationEntity> relationList = attrAttrgroupRelationService.list(relationWrapper);
        //将relationList中的所有attr_id放在set集合中
        Set<Long> attrIdSet = new HashSet<>();
        for (AttrAttrgroupRelationEntity relation : relationList) {
            attrIdSet.add(relation.getAttrId());
        }
        attrList.removeIf(attr -> attrIdSet.contains(attr.getAttrId()));

        Page page = new Page();
        page.setCurrent(Integer.parseInt(params.get("page").toString()));
        page.setTotal(attrList.size());
        page.setSize(Integer.parseInt(params.get("limit").toString()));
        page.setPages(attrList.size() % page.getSize() == 0 ? attrList.size() / page.getSize() : attrList.size() / page.getSize() + 1);
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(attrList);
        return pageUtils;
    }

    @Override
    public void attrgroupAttrRelation(List<Map<String, Long>> relations) {
        for (Map<String, Long> params : relations) {
            Long attrId = params.get("attrId");
            Long attrGroupId = params.get("attrGroupId");
            if (attrId != null && attrGroupId != null) {
                AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity();
                relation.setAttrId(attrId);
                relation.setAttrGroupId(attrGroupId);
                attrAttrgroupRelationService.save(relation);
            }
        }
    }

    @Override
    public void attrgroupAttrRelationDelete(List<Map<String, Long>> relations) {
        if (relations == null) {
            return;
        }
        for (Map<String, Long> params : relations) {
            Long attrId = params.get("attrId");
            Long attrGroupId = params.get("attrGroupId");
            if (attrGroupId != null && attrId != null) {
                QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("attr_id", attrId);
                wrapper.eq("attr_group_id", attrGroupId);
                attrAttrgroupRelationService.remove(wrapper);
            }
        }
    }

    @Override
    public List<AttrGroupEntity> catelogIdWithAttr(Long catelogId) {
        //查询属性分下所有catelogId为指定值的AttrGroup
        QueryWrapper<AttrGroupEntity> attrGroupWrapper = new QueryWrapper<>();
        attrGroupWrapper.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroups = list(attrGroupWrapper);
        //查询出每个attrGroup下的所有attr，封装到AttrGroupVo中
        for (AttrGroupEntity attrGroup : attrGroups) {
            QueryWrapper<AttrAttrgroupRelationEntity> relationWrapper = new QueryWrapper<>();
            relationWrapper.eq("attr_group_id", attrGroup.getAttrGroupId());
            List<AttrAttrgroupRelationEntity> relations = attrAttrgroupRelationService.list(relationWrapper);
            for (AttrAttrgroupRelationEntity relation : relations) {
                QueryWrapper<AttrEntity> attrWrapper = new QueryWrapper<>();
                attrWrapper.eq("attr_id", relation.getAttrId());
//                attrWrapper.eq("attr_type",1);
                AttrEntity attr = attrService.getOne(attrWrapper);
                attrGroup.addAttr(attr);
            }
        }
        return attrGroups;
    }
}
